package com.demo.modules.controller;

import com.demo.common.Res;
import com.demo.common.utils.UserUtils;
import com.demo.modules.dto.QueryDTO;
import com.demo.modules.entity.Register;
import com.demo.modules.entity.VaccinationRecord;
import com.demo.modules.entity.VaccineAppointment;
import com.demo.modules.service.VaccineAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 疫苗预约记录表
 */
@RestController
@RequestMapping("appointment")
public class VaccineAppointmentController {

    @Autowired
    private VaccineAppointmentService vaccineAppointmentService;

    /**
     * 查询疫苗预约记录列表数据
     * @return
     */
    @GetMapping("list")
    public Res<List<VaccineAppointment>> list(){
        QueryDTO dto = new QueryDTO();
        dto.setUserId(UserUtils.getUserId());
        return Res.success(vaccineAppointmentService.list(dto));
    }

    /**
     * 根据id值在表里是否存在判断是新增还是修改操作
     * @param entity
     * @return
     */
    @PostMapping("saveOrUpdate")
    public Res<String> saveOrUpdate(@RequestBody VaccineAppointment entity){
        if(entity.getUserId() == null){
            entity.setUserId(UserUtils.getUserId());
        }
        boolean result = vaccineAppointmentService.saveOrUpdate(entity);
        if(result){
            return Res.successMsg("新增或修改成功");
        }
        return Res.errorMsg("新增或修改失败");
    }

}
